package com.example.administrator.familfinalcialmanagement.ui.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.example.administrator.familfinalcialmanagement.R;
import com.example.administrator.familfinalcialmanagement.bean.CommonUser;
import com.example.administrator.familfinalcialmanagement.bean.GreenDaoUtils;
import com.example.administrator.familfinalcialmanagement.bean.JaveTeacher;
import com.example.administrator.familfinalcialmanagement.databinding.ActivityRegisterBinding;
import com.example.administrator.familfinalcialmanagement.greendao.gen.CommonUserDao;
import com.example.administrator.familfinalcialmanagement.greendao.gen.JaveTeacherDao;
import com.example.administrator.familfinalcialmanagement.greendao.gen.SuperManagerDao;
import com.example.administrator.familfinalcialmanagement.ui.BaseActivity;
import com.example.administrator.familfinalcialmanagement.ui.activity.TeacherDescActivity;
import com.example.administrator.familfinalcialmanagement.ui.adapter.CommonUserAdapter;
import com.example.administrator.familfinalcialmanagement.ui.adapter.TeacherDescAdapter;
import com.example.administrator.familfinalcialmanagement.util.LogUtil;
import com.example.administrator.familfinalcialmanagement.util.SpUtils;
import com.example.administrator.familfinalcialmanagement.util.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2019/3/26.
 */

public class RegisterActivity extends BaseActivity {

    ActivityRegisterBinding binding;
        String phone, pwd, type, name;

        CommonUserDao commonUserDao = GreenDaoUtils.getSingleTon().getmDaoSession().getCommonUserDao();
        JaveTeacherDao teacherDao = GreenDaoUtils.getSingleTon().getmDaoSession().getJaveTeacherDao();
        SuperManagerDao superManagerDao = GreenDaoUtils.getSingleTon().getmDaoSession().getSuperManagerDao();

        TeacherDescAdapter teacherDescAdapter;
        List<JaveTeacher> teacherList = new ArrayList<>();

        CommonUserAdapter commonUserAdapter;
        List<CommonUser> commonUserList = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        binding = DataBindingUtil.setContentView( this, R.layout.activity_register );
        binding.setActivity( this );
                initView();
    }

    private void initView() {
        setBack();
        setTitleText( "注册" );
        initData();
    }

    private void initData() {

                phone = (String) SpUtils.get( "module_phone", "" );
                pwd = (String) SpUtils.get( "module_pwd", "" );

                LogUtil.e( "phone-->" + phone );
                LogUtil.e( "pwd-->" + pwd );

                LogUtil.e( "科目管理者-->" + teacherDao.loadAll().size() + "人" );
                LogUtil.e( "普通用户-->" + commonUserDao.loadAll().size() + "人" );

                String isLogin = (String) SpUtils.get( "isLogin", "" );
                LogUtil.e( "isLogin-->" + isLogin );

                if ("1".equals( isLogin )) {
                    LogUtil.e( "是超级管理员" );
                    superMethod();
                    setTitleText( superManagerDao.loadAll().get( 0 ).getName() );
                } else if ("2".equals( isLogin )) {
                    LogUtil.e( "是科目管理者" );
                    for (JaveTeacher teacher : teacherDao.loadAll()) {
                        if (teacher.getPhone().equals( phone ) && teacher.getPwd().equals( pwd )) {
                            teacherMethod( teacher.className );
                            LogUtil.e( "科目名称-->" + teacher.className );
                            name = teacher.getName();
                        }
                    }
                    setTitleText( name );
                }
    }

    public void superMethod() {
        if (teacherDao.loadAll().size() != 0) {
            teacherList.addAll(teacherDao.loadAll());
        }
        teacherDescAdapter = new TeacherDescAdapter(this, teacherList);
        binding.recyclerTeacher.setLayoutManager(new LinearLayoutManager(this));
        binding.recyclerTeacher.setItemAnimator(new DefaultItemAnimator());
        binding.recyclerTeacher.setAdapter(teacherDescAdapter);
        teacherDescAdapter.setOnItemClickLitener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent2 = new Intent(RegisterActivity.this, TeacherDescActivity.class);
                intent2.putExtra("module_phone", teacherList.get(position).getPhone());
                intent2.putExtra("module_pwd", teacherList.get(position).getPwd());
                startActivity(intent2);
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });


        if (commonUserDao.loadAll().size() != 0) {
            commonUserList.addAll(commonUserDao.loadAll());
        }
        commonUserAdapter = new CommonUserAdapter(this, commonUserList);
        binding.recyclerCommon.setLayoutManager(new LinearLayoutManager(this));
        binding.recyclerCommon.setItemAnimator(new DefaultItemAnimator());
        binding.recyclerCommon.setAdapter(commonUserAdapter);

        commonUserAdapter.setOnItemClickLitener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent2 = new Intent(RegisterActivity.this, MyInfoActivity.class);
                intent2.putExtra("module_phone", commonUserList.get(position).getPhone());
                intent2.putExtra("module_pwd", commonUserList.get(position).getPwd());
                startActivity(intent2);
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });
    }

    public void teacherMethod(String className) {
        commonUserList.clear();
        commonUserAdapter = new CommonUserAdapter(this, commonUserList);
        binding.recyclerCommon.setLayoutManager(new LinearLayoutManager(this));
        binding.recyclerCommon.setItemAnimator(new DefaultItemAnimator());
        binding.recyclerCommon.setAdapter(commonUserAdapter);
        commonUserAdapter.setOnItemClickLitener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent2 = new Intent(RegisterActivity.this, MyInfoActivity.class);
                intent2.putExtra("module_phone", commonUserList.get(position).getPhone());
                intent2.putExtra("module_pwd", commonUserList.get(position).getPwd());
                startActivity(intent2);
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });

        for (CommonUser user : commonUserDao.loadAll()) {
            /**
             * 使用CommonUser表中占位one 表示当前显示的科目，以便隐藏其他科目
             */
            LogUtil.e("添加-->" + user.getName());
            user.setOne(className);
            commonUserList.add(user);
            commonUserAdapter.notifyDataSetChanged();
        }
    }

}
